﻿using System;
using System.Collections.Generic;
using System.IO;
using Velocity4Net.Ctx;
using Velocity4Net.Runtime.Parser;
using Velocity4Net.Runtime.Parser.Node;

namespace Velocity4Net.Runtime.Directives
{

    /*
     * Licensed to the Apache Software Foundation (ASF) under one
     * or more contributor license agreements.  See the NOTICE file
     * distributed with this work for additional information
     * regarding copyright ownership.  The ASF licenses this file
     * to you under the Apache License, Version 2.0 (the
     * "License"); you may not use this file except in compliance
     * with the License.  You may obtain a copy of the License at
     *
     *   http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing,
     * software distributed under the License is distributed on an
     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     * KIND, either express or implied.  See the License for the
     * specific language governing permissions and limitations
     * under the License.
     */












    /**
     * Break directive used for interrupting scopes.
     *
     * @author <a href="mailto:wyla@removethis.sci.fi">Jarkko Viinamaki</a>
     * @author Nathan Bubna
     * @version $Id$
     */
    public class Break:Directive
{
    private bool scoped = false;

    /**
     * Return name of this directive.
     * @return The name of this directive.
     */
    public String getName()
    {
        return "break";
    }

    /**
     * Return type of this directive.
     * @return The type of this directive.
     */
    public int getType()
    {
        return LINE;
    }

    /**
     * Since there is no processing of content,
     * there is never a need for an internal scope.
     */
    public bool isScopeProvided()
    {
        return false;
    }

    
    public override void init(RuntimeServices rs, InternalContextAdapter context, Node node)
    {
        super.init(rs, context, node);

        this.scoped = (node.jjtGetNumChildren() == 1);
    }

    /**
     * This directive throws a StopCommand which signals either
     * the nearest Scope or the specified scope to stop rendering
     * its content.
     * @return never, always throws a StopCommand or Exception
     */
    public bool render(InternalContextAdapter context, TextWriter writer, Node node)
    {
        if (!scoped)
        {
            throw new StopCommand();
        }

        Object argument = node.jjtGetChild(0).value(context);
        if (argument is Scope)
        {
            ((Scope)argument).stop();
            throw new IllegalStateException("Scope.stop() failed to throw a StopCommand");
        }
        else
        {
            throw new VelocityException(node.jjtGetChild(0).literal()+
                " is not a valid " + nameof(Scope) + " instance at "
                + StringUtils.formatFileString(this),
                null,
                rsvc.getLogContext().getStackTrace());
        }
    }

    /**
     * Called by the parser to validate the argument types
     */
    public void checkArgs(List<int> argtypes,  Token t, String templateName)
        {
        if (argtypes.Count > 1)
        {
            throw new MacroParseException("The #break directive takes only a single, optional Scope argument",
               templateName, t);
        }
    }

}
}